/**
 * 
 */
package br.com.ufrn.aipetro.ga.controller.interfaces;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import br.com.ufrn.aipetro.ga.domain.entity.interfaces.Model;
import br.com.ufrn.aipetro.ga.view.interfaces.View;

/**
 * @author Lucas
 *
 */
public class AbstractController<T extends Model> implements Controller<T> {

	private T model;
	private List<View> registeredViews;
	
	public AbstractController() {
		registeredViews = new ArrayList<View>();
	}
	
	@Override
	public T getModel() {
		return model;
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		for (View view : registeredViews) {
			view.propertyChange(evt);
		}	
	}

	@Override
	public void registerView(View view) {
		registeredViews.add(view);
	}

	@Override
	public void removeModel() {
		this.model = null;
		model.removeListener(this);
	}

	@Override
	public void removeView(View view) {
		registeredViews.remove(view);
	}
	
	@Override
	public void setModel(T model) {
		this.model = model;
		model.addListener(this);
		for (View view : registeredViews) {
			view.modelChange();
		}
	}

}
